﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using Phoenix.AppCode.Entities;
using System.Data.SqlClient;
using System.Data;

namespace Phoenix.AppCode.Data
{
    /// <summary>
    /// Summary description for Article Category
    /// </summary>
    public class ArticleCategoryDAO :BaseDAO
    {
        //
        // TODO: Add constructor logic here
        //


        /// <summary>
        /// Insert Article Category
        /// </summary>
        /// <param name="articlecategory"></param>
        public void Create(ArticleCategory articlecategory)
        {
            using (SqlConnection connection = GetConnection())
            {
                SqlCommand command = new SqlCommand("spArticleCategoryCreate", connection);
                command.CommandType = CommandType.StoredProcedure;

                command.Parameters.Add(new SqlParameter("@Name_vi", articlecategory.Name_vi));
                command.Parameters.Add(new SqlParameter("@Name_en", articlecategory.Name_en));

                connection.Open();
                command.ExecuteNonQuery();
                //if (paramId.Value != DBNull.Value)
                    //articlecategory.ID = (long)paramId.Value;
                //else
                  //  throw new DataAccessException(String.Format(Resources.Strings.CreateEntityException, "Category"));
            }
        }

        /// <summary>
        /// Edit Article Category
        /// </summary>
        /// <param name="category"></param>
        public void Edit(ArticleCategory articlecategory)
        {
            using (SqlConnection connection = GetConnection())
            {
                SqlCommand command = new SqlCommand("spArticleCategoryEdit", connection);
                command.CommandType = CommandType.StoredProcedure;

                command.Parameters.Add(new SqlParameter("@ID_ArticleCategory", articlecategory.ID_ArticleCategory));
                command.Parameters.Add(new SqlParameter("@Name_vi", articlecategory.Name_vi));
                command.Parameters.Add(new SqlParameter("@Name_en", articlecategory.Name_en));

                connection.Open();
                command.ExecuteNonQuery();
                //if (command.ExecuteNonQuery() <= 0)
                //    throw new DataAccessException(String.Format(Resources.Strings.UpdateEntityException, "Category", category.ID));
            }
        }
        
        /// <summary>
        /// On/Off view
        /// </summary>
        /// <param name="articlecategory"></param>
        public void OnOff(ArticleCategory articlecategory)
        {
            using (SqlConnection connection = GetConnection())
            {
                SqlCommand command = new SqlCommand("spArticleCategoryOnOff", connection);
                command.CommandType = CommandType.StoredProcedure;

                command.Parameters.Add(new SqlParameter("@ID_ArticleCategory", articlecategory.ID_ArticleCategory));

                connection.Open();
                command.ExecuteNonQuery();
                //if (command.ExecuteNonQuery() <= 0)
                //    throw new DataAccessException(String.Format(Resources.Strings.UpdateEntityException, "Category", category.ID));
            }
        }


        /// <summary>
        /// Delete Article Category
        /// </summary>
        /// <param name="category"></param>
        public void Delete(ArticleCategory articlecategory)
        {
            using (SqlConnection connection = GetConnection())
            {
                SqlCommand command = new SqlCommand("spArticleCategoryDelete", connection);
                command.CommandType = CommandType.StoredProcedure;

                command.Parameters.Add(new SqlParameter("@ID_ArticleCategory", articlecategory.ID_ArticleCategory));

                connection.Open();
                command.ExecuteNonQuery();
                //if (command.ExecuteNonQuery() <= 0)
                //    throw new DataAccessException(String.Format(Resources.Strings.DeleteEntityException, "Category", category.ID));
            }
        }

        /// <summary>
        /// Show Article Category All
        /// </summary>
        /// <returns></returns>
        public DataTable ArticleCategoryList(String language)
        {
            using (SqlConnection connection = GetConnection())
            {
                SqlCommand command = new SqlCommand("spArticleCategoryList", connection);
                command.CommandType = CommandType.StoredProcedure;
                command.Parameters.Add("@Language", SqlDbType.NVarChar).Value = language;
                SqlDataAdapter da = new SqlDataAdapter(command);

                connection.Open();
                DataTable dt = new DataTable();
                da.Fill(dt);
                
                return dt;
            }
        }

        /// <summary>
        /// Show data by ID
        /// </summary>
        /// <param name="reader"></param>
        /// <returns></returns>
        public DataTable ArticleCategoryList_GetByID(int idarticlecategory)
        {
            using (SqlConnection connection = GetConnection())
            {
                SqlCommand command = new SqlCommand("spArticleCategoryEdit_GetByID", connection);
                command.CommandType = CommandType.StoredProcedure;
                command.Parameters.Add("@ID_ArticleCategory", SqlDbType.Int).Value = idarticlecategory;
                SqlDataAdapter da = new SqlDataAdapter(command);

                connection.Open();
                DataTable dt = new DataTable();
                da.Fill(dt);
                
                return dt;
            }
        }

        /// <summary>
        /// Get ID to Edit or Delete
        /// </summary>
        /// <param name="idarticlecategory"></param>
        /// <returns></returns>
        public ArticleCategory GetArticleCategoryByID(int idarticlecategory)
        {
            using (SqlConnection connection = GetConnection())
            {
                SqlCommand command = new SqlCommand("spArticleCategoryEdit_GetByID", connection);
                command.CommandType = CommandType.StoredProcedure;

                command.Parameters.Add(new SqlParameter("@ID_ArticleCategory", idarticlecategory));

                connection.Open();
                ArticleCategory articlecategory = null;
                using (IDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection))
                {
                    if (reader.Read())
                        articlecategory = CreateArticleCategoryFromReader(reader);
                    //else
                     //   throw new DataAccessException(String.Format(Resources.Strings.GetEntityException, "Category", ID));
                }

                return articlecategory;
            }
        }

        private ArticleCategory CreateArticleCategoryFromReader(IDataReader reader)
        {
            ArticleCategory articlecategory = new ArticleCategory();
            articlecategory.ID_ArticleCategory = (int)reader["ID_ArticleCategory"];
            articlecategory.Name = (string)reader["Name"];
            articlecategory.Visible = (Boolean)reader["Visible"];
            articlecategory.Language = (string)reader["Language"];
            return articlecategory;
        }
    }
}